package com.lzk.auth.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.lzk.auth.service.ProductService;
import com.lzk.auth.service.TypeService;
import com.lzk.common.result.Result;
import com.lzk.model.system.SysProduct;


import com.lzk.model.system.SysType;

import com.lzk.vo.system.ProductVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lzk
 * @since 2023-12-230
 */
@Api(tags = "产品管理")
@RestController
@RequestMapping("/admin/system/product")
public class ProductController {
    @Autowired
    private ProductService productService;

    @Autowired
    private TypeService typeService;

    @ApiOperation("产品分页查询")
    @GetMapping("{page}/{limit}")
    public Result index(@PathVariable Long page,
                        @PathVariable Long limit,
                        ProductVo productVo
    ) {
        Page<SysProduct> pageParam = new Page<>(page, limit);

        LambdaQueryWrapper<SysProduct> wrapper = new LambdaQueryWrapper<>();


        Integer ptype = productVo.getPtype();
        if (null != ptype) {
            wrapper.eq(SysProduct::getPType,ptype);
            System.out.println("-----------------");
            System.out.println(ptype);
        }
        String pname = productVo.getPname();
        if (!StringUtils.isEmpty(pname)) {
            wrapper.like(SysProduct::getPName,pname);
            System.out.println("-----------------");
            System.out.println(pname);
        }

        IPage<SysProduct> pageModel = productService.page(pageParam, wrapper);
        List<SysProduct> records = pageModel.getRecords();
        for (SysProduct record : records) {
            Integer pType = record.getPType();
            SysType type = typeService.getById(pType);
            record.setRoleName(type.getName());
        }

        return Result.ok(pageModel);
    }

    @ApiOperation(value = "根据id查产品")
    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id) {
        SysProduct product = productService.getById(id);
        return Result.ok(product);
    }

    @ApiOperation(value = "录入产品信息")
    @PostMapping("save")
    public Result save(@RequestBody SysProduct product) {

        productService.save(product);
        return Result.ok();
    }

    @ApiOperation(value = "更新产品信息")
    @PutMapping("update")
    public Result updateById(@RequestBody SysProduct product) {
        productService.updateById(product);
        return Result.ok();
    }

    @ApiOperation(value = "删除产品")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id) {

        UpdateWrapper<SysProduct> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id); // 根据ID作为更新条件

        updateWrapper.set("is_deleted", 1);
        productService.update(updateWrapper);

        return Result.ok();
    }


}


